# Make LaTeX chunk for Table 1

# Load Models and Store AUCs
countries <- c('indo', 'colo')
dvs <- c("any", "high", "spike")
algos <- c('nn', 'nn2', 'nn3', 
           'nn4', 
           'nn5'
           )

auc_array <- array(NA,
                   dim = c(length(countries),
                           length(dvs),
                           length(algos)),
                   dimnames = list(countries,
                                   dvs,
                                   algos))
for (country in countries) {
  for (algo in algos) {
    modelname = algo
    for (dv in dvs) {
      filename <- paste(modeldir,"/",
                    country,
                    "_",algo,"_",
                    dv,
                    "_full.RData",
                    sep = "")
      load(filename)
      aucs_by_year = c()

      for (i in 1:length(get(paste("nn",".results",sep="")))) {
          predictions <- as.vector(get(paste("nn",".results",sep=""))[[i]]$fit.oos)
          realizations <- as.vector(get(paste("nn",".results",sep=""))[[i]]$actual.oos)
          aucs_by_year <- c(aucs_by_year,
                            roc(response = realizations,
                                predictor = predictions)[['auc']])
      }
      auc_array[country, dv, algo] <- mean(aucs_by_year)
    }
  }
}


# Build Table with Labels
country.labels <- c(indo='Indonesia',
                    colo='Colombia')
outcome.labels <- c(any="Any violent event",
                    high="$\\ge$ 5 violent events",
                    spike="$\\ge$ 1 s.d. increase in events")
model.labels <- c(nn='Baseline\\\\ PC Step \\\\ 1 layer \\\\ nodes by CV \\\\ 0 Regularization',
                  nn2='No PC Step \\\\ 1 layer \\\\ 7 nodes \\\\ 0.15 Regularization',
                  nn3='No PC Step \\\\ 1 layer \\\\ 7 nodes \\\\ 0 Regularization',
                  nn4='No PC Step \\\\ 2 layers \\\\ 10,5 nodes \\\\ 0 Regularization',
                  nn5='No PC Step \\\\ 2 layers \\\\ 256, 128 nodes \\\\ .5, .3 dropout')

table <- auc_array
dimnames(table)[[1]] <- country.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- outcome.labels[dimnames(table)[[2]]]
dimnames(table)[[3]] <- model.labels[dimnames(table)[[3]]]

# Print to Latex
print.texfragment(table = table,
                  filepath = "tables",
                  file = "table_B2",
                  head.height="2.8",
                  head.width="2.5")

